//
//  SinaMainController.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/22.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit
import SVProgressHUD

class SinaMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //实例化 tabbar
        
        let tabBar = SinaTabBar()
        //2.实例化闭包
        tabBar.closure = { [unowned self] in
            //4.闭包的代码块
            print("点击按钮")
            //判断是否登录
            if !SinaUserAccountViewModel.sharedTools.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录")
                return
            }
            //表示已经登录
            
            let composeView = SinaCompose()
            composeView.show(target: self)
        }

        setValue(tabBar, forKey: "tabBar")
        addChildViewController(vc: SinaHomeController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(vc: SinaMessageController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(vc: SinaDiscoverController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(vc: SinaProfileController(), title: "我", imgName: "tabbar_profile")
        
    }
    
    
    /// 创建子控制器的主方法
    ///
    /// - parameter vc:      传入的控制器
    /// - parameter title:   名字
    /// - parameter imgName: 图片
    func addChildViewController(vc: UIViewController, title: String, imgName: String) {
        //设置 title
        vc.title = title
        
        //设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: SinaColor], for: UIControlState.selected)
        //设置图片
        vc.tabBarItem.image = UIImage(named: imgName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //创建一个导航控制器
        let nav = SinaNavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
}
